import request from '@/utils/request';
import { convertPageInfo, convertPostList } from '@/api/formatter';

//根据cid和分页数据来获取文章列表信息
export function getPostListByCid(cid: number, pageSize: number, pageIndex = 1) {
    return request({
        url: '/news/categoryList/' + cid,
        method: 'GET',
        params: {
            page: pageIndex,
            limit: pageSize,
        },
    }).then((data: any) => {
        if (data.staffList) {
            return {
                staffList: data.staffList,
            };
        }
        return {
            postList: convertPostList(data),
            pageInfo: convertPageInfo(data),
        };
    });
}

//获取大类最近更新时间
export function getRecentUpdateTimeByCid(cid: number) {
    return request({
        url: '/category/' + cid + '/recentUpdateTime',
        method: 'GET',
    }).then((data: any) => {
        return data.recentUpdateTime;
    });
}

//获取最近更新列表
export function getLatestPosts(pageSize: number, pageIndex = 1) {
    return request({
        url: '/news/latest',
        method: 'GET',
        params: {
            page: pageIndex,
            limit: pageSize,
        },
    }).then((data: any) => {
        return {
            postList: convertPostList(data, false),
            pageInfo: convertPageInfo(data),
            recentUpdate: data.recentUpdateTime,
        };
    });
}
